/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import db.Redis_Rd;
import db.Redis_W;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Map;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import share.KeysDefinition;
import share.ShareMacros;

/**
 *
 * @author Mrkupi
 */
public class LoadUserForm extends javax.swing.JFrame {
    
    private Map<String, String> mValueUser;
    /**
     * Creates new form LoadUserForm
     */
    public LoadUserForm() {
        initComponents();
        setLocation( 500, 200 );
        this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                dispose();
            }
        });
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        tf_UserName = new javax.swing.JTextField();
        btn_Load = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        tf_Level = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        tf_Fish = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tf_Gold = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tf_Coin = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tf_Exp = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        tf_Name = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        tf_HelpFood = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        tf_HelpDay = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ls_Cotes = new javax.swing.JList();
        jLabel10 = new javax.swing.JLabel();
        btn_Update = new javax.swing.JButton();
        btn_Cancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LOAD USER INFO");

        jLabel1.setText("User Name");

        tf_UserName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tf_UserNameKeyPressed(evt);
            }
        });

        btn_Load.setText("Load");
        btn_Load.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_LoadMouseClicked(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("User Info"));

        jLabel2.setText("Level");

        jLabel3.setText("Fish");

        jLabel4.setText("Gold");

        jLabel5.setText("Coin");

        jLabel6.setText("Exp");

        jLabel7.setText("Name");

        jLabel8.setText("Help Food");

        jLabel9.setText("Help Day");

        ls_Cotes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ls_CotesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(ls_Cotes);

        jLabel10.setText("Cotes");

        btn_Update.setText("Update");
        btn_Update.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_UpdateMouseClicked(evt);
            }
        });

        btn_Cancel.setText("Cancel");
        btn_Cancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btn_CancelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btn_Update)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                        .addComponent(btn_Cancel))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(8, 8, 8)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(tf_Level, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel6)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(2, 2, 2)
                                        .addComponent(jLabel3))
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tf_Gold)
                                .addComponent(tf_Coin)
                                .addComponent(tf_Fish)
                                .addComponent(tf_Exp, javax.swing.GroupLayout.DEFAULT_SIZE, 123, Short.MAX_VALUE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tf_Name, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                    .addComponent(tf_HelpFood)
                    .addComponent(tf_HelpDay)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tf_Level, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(tf_Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_Fish, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel8)
                    .addComponent(tf_HelpFood, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_Gold, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel9)
                    .addComponent(tf_HelpDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(tf_Coin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6)
                            .addComponent(tf_Exp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btn_Update)
                            .addComponent(btn_Cancel)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(tf_UserName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btn_Load, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_UserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_Load))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_LoadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_LoadMouseClicked
        
        if( tf_UserName.getText().equals("") )
            return;
        
        String keyUser  =   KeysDefinition.getKeyUser( tf_UserName.getText() );
        mValueUser      =   (Map<String, String>) Redis_Rd.getInstance().hget(keyUser);
        if( mValueUser == null || mValueUser.size() <= 0 )
            return;
        
        tf_Level.setText( String.valueOf(mValueUser.get( ShareMacros.LEVEL ) ) );
        tf_Fish.setText( String.valueOf(mValueUser.get( ShareMacros.FISH )));
        tf_Gold.setText( String.valueOf(mValueUser.get(ShareMacros.GOLD)) );
        tf_Coin.setText( String.valueOf(mValueUser.get(ShareMacros.COIN)));
        tf_Exp.setText( String.valueOf(mValueUser.get(ShareMacros.EXP)) );
        tf_Name.setText( String.valueOf(mValueUser.get(ShareMacros.USER_NAME)));
        tf_HelpFood.setText( String.valueOf(mValueUser.get(ShareMacros.USER_HELPFOOD)));
        tf_HelpDay.setText( String.valueOf(mValueUser.get(ShareMacros.USER_HELPDAY)));
        
        // Load cote
        DefaultListModel listModel   = new DefaultListModel();
        Set<String> lsCotes     =   Redis_Rd.getInstance().smember( String.valueOf( mValueUser.get(ShareMacros.COTES)));
        for( String str : lsCotes )
        {
            listModel.addElement( str );
        }
        ls_Cotes.setModel(listModel);
    }//GEN-LAST:event_btn_LoadMouseClicked

    private void tf_UserNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tf_UserNameKeyPressed
        if( evt.getKeyChar() == java.awt.event.KeyEvent.VK_ENTER )
        {
            btn_LoadMouseClicked( null );
        }
    }//GEN-LAST:event_tf_UserNameKeyPressed

    private void ls_CotesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ls_CotesMouseClicked
        if (evt.getClickCount() == 2) 
        {
            String selected =   (String)ls_Cotes.getSelectedValue();
            LoadCoteInfo lCote  =   new LoadCoteInfo( selected );
            lCote.setVisible( true );
        }
    }//GEN-LAST:event_ls_CotesMouseClicked

    private void btn_CancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_CancelMouseClicked
        tf_UserName.setText( "" );
        tf_Level.setText( "" );
        tf_Fish.setText( "" );
        tf_Gold.setText( "" );
        tf_Coin.setText( "" );
        tf_Exp.setText( "" );
        tf_Name.setText( "" );
        tf_HelpFood.setText( "" );
        tf_HelpDay.setText( "" );
    }//GEN-LAST:event_btn_CancelMouseClicked

    private void btn_UpdateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btn_UpdateMouseClicked
        
        if( tf_UserName.getText().equals("") || tf_Level.getText().equals("") || tf_Fish.getText().equals("") || 
                tf_Gold.getText().equals("") || tf_Coin.getText().equals("") || tf_Exp.getText().equals("")  || tf_Name.getText().equals("")  || 
                tf_HelpFood.getText().equals("") || tf_HelpDay.getText().equals(""))
            return;
        
        mValueUser.put(ShareMacros.FISH, tf_Fish.getText());
        mValueUser.put(ShareMacros.GOLD,  tf_Gold.getText());
        mValueUser.put(ShareMacros.COIN, tf_Coin.getText());
        mValueUser.put(ShareMacros.EXP, tf_Exp.getText());
        mValueUser.put(ShareMacros.LEVEL, tf_Level.getText());
        mValueUser.put(ShareMacros.USER_NAME, tf_Name.getText());
        mValueUser.put(ShareMacros.USER_HELPFOOD, tf_HelpFood.getText());
        mValueUser.put(ShareMacros.USER_HELPDAY, tf_HelpDay.getText());
        
        String keyUser  =   KeysDefinition.getKeyUser( tf_UserName.getText() );
        String result   =   Redis_W.getInstance().hset( keyUser, mValueUser );
        if( !result.equals("") )
            JOptionPane.showMessageDialog(this, "Done");
    }//GEN-LAST:event_btn_UpdateMouseClicked

//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(LoadUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(LoadUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(LoadUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(LoadUserForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new LoadUserForm().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_Cancel;
    private javax.swing.JButton btn_Load;
    private javax.swing.JButton btn_Update;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList ls_Cotes;
    private javax.swing.JTextField tf_Coin;
    private javax.swing.JTextField tf_Exp;
    private javax.swing.JTextField tf_Fish;
    private javax.swing.JTextField tf_Gold;
    private javax.swing.JTextField tf_HelpDay;
    private javax.swing.JTextField tf_HelpFood;
    private javax.swing.JTextField tf_Level;
    private javax.swing.JTextField tf_Name;
    private javax.swing.JTextField tf_UserName;
    // End of variables declaration//GEN-END:variables
}
